# -*- coding: utf-8 -*-
import scrapy, re
from weather.items import WeatherItem

class allweather(scrapy.spiders.Spider):
    """docstring for kuWo."""
    name = 'weather'
    start_urls = ['http://lishi.tianqi.com/']

    def parse(self,response):
        for sel in response.xpath("/html/body/div[@id='cityall']/div[@id='tool_site']/div/ul/li/a"):
            url = sel.xpath("@href").extract()[0]
            if url!='#':
                yield scrapy.Request(url, callback=self.citydetail)
            pass

    def citydetail(self,response):
        for sel in response.xpath("/html/body/div[@id='main']/div[@class='left']/div[@id='tool_site']/div[@class='tqtongji1']/ul/li/a"):
            url = sel.xpath('@href').extract()[0]
            yield scrapy.Request(url, callback=self.daydetail)

    def daydetail(self,response):
        item = WeatherItem()
        for sel in response.xpath("/html/body/div[@id='main']/div[@class='left']/div[@id='tool_site'][2]/div[@class='tqtongji2']/ul"):
            try:
                item['date'] = sel.xpath("li[1]/text()").extract()[0]
            except Exception as e:
                item['date'] = sel.xpath("li[1]/a/text()").extract()[0]
            item['city'] = sel.xpath("/html/body/div[@class='header']/div[@class='city_t pageclear']/div[@class='city_title clearfix']/table[@class='ad']/tbody/tr/td[@class='tdleft']/h9[@class='tqnav_22']/a[3]/text()").extract()[0].replace('历史天气','')
            item['highestTemperature'] = sel.xpath("li[2]").extract()[0].replace('<li>','').replace('</li>','')
            item['lowTemperature'] = sel.xpath("li[3]").extract()[0].replace('<li>','').replace('</li>','')
            item['weather'] = sel.xpath("li[4]").extract()[0].replace('<li>','').replace('</li>','')
            item['windDirection'] = sel.xpath("li[5]").extract()[0].replace('<li>','').replace('</li>','')
            item['windPower'] = sel.xpath("li[6]").extract()[0].replace('<li>','').replace('</li>','')
            if item['date'] !='日期':
                yield item
        # pass
